package cn.edu.dutir.test.unit.parser;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import cn.edu.dutir.corpus.trec.TrecTopic;
import cn.edu.dutir.corpus.trec.TrecTopicHandler;

import com.aliasi.util.Streams;

public class TrecTopicWriterHandler implements TrecTopicHandler {

	private Writer mWriter;

	public TrecTopicWriterHandler(Writer writer) {
		mWriter = writer;
	}

	public TrecTopicWriterHandler(File outFile) throws IOException {
		this(new BufferedWriter(new FileWriter(outFile)));
	}

	@Override
	public void handle(TrecTopic topic) {
		try {
			mWriter.write(topic.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void flush() {
		try {
			mWriter.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void close() {
		flush();
		Streams.closeWriter(mWriter);
	}

}
